import { HistoryManager } from './HistoryManager'
import { SelectionHandler } from './SelectionHandler'
import { FormatHandler } from './FormatHandler'

export class EditorCore {
  constructor(editorElement) {
    this.editorElement = editorElement
    this.historyManager = new HistoryManager()
    this.selectionHandler = new SelectionHandler(editorElement)
    this.formatHandler = new FormatHandler(this)

    this.setupListeners()
    this.setupShortcuts()
  }

  setupListeners() {
    this.editorElement.addEventListener('input', () => {
      this.historyManager.recordState(this.getContent())
    })
  }

  setupShortcuts() {
    this.editorElement.addEventListener('keydown', (e) => {
      if (e.ctrlKey || e.metaKey) {
        switch (e.key) {
          case 'z':
            if (e.shiftKey) this.redo()
            else this.undo()
            e.preventDefault()
            break
          case 'y':
            this.redo()
            e.preventDefault()
            break
        }
      }
    })
  }

  // 核心编辑器方法
  getContent() {
    return this.editorElement.innerHTML
  }

  setContent(content) {
    this.editorElement.innerHTML = content
    this.historyManager.recordState(content)
  }

  undo() {
    const state = this.historyManager.undo()
    if (state) {
      this.editorElement.innerHTML = state.content
      return {
        success: true,
        canUndo: this.historyManager.canUndo,
        canRedo: this.historyManager.canRedo
      }
    }
    return {
      success: false,
      canUndo: this.historyManager.canUndo,
      canRedo: this.historyManager.canRedo
    }
  }

  redo() {
    const state = this.historyManager.redo()
    if (state) {
      this.editorElement.innerHTML = state.content
      return {
        success: true,
        canUndo: this.historyManager.canUndo,
        canRedo: this.historyManager.canRedo
      }
    }
    return {
      success: false,
      canUndo: this.historyManager.canUndo,
      canRedo: this.historyManager.canRedo
    }
  }

  lockSelection() {
    const result = this.selectionHandler.lockSelection()
    if (result) {
      this.historyManager.recordState(this.getContent())
      return {
        success: true,
        canUndo: this.historyManager.canUndo,
        canRedo: this.historyManager.canRedo
      }
    }
    return {
      success: false,
      canUndo: this.historyManager.canUndo,
      canRedo: this.historyManager.canRedo
    }
  }

  execFormatCommand(name, ...args) {
    const result = this.formatHandler.executeCommand(name, ...args)
    if (result) {
      this.historyManager.recordState(this.getContent())
      return {
        success: true,
        canUndo: this.historyManager.canUndo,
        canRedo: this.historyManager.canRedo
      }
    }
    return {
      success: false,
      canUndo: this.historyManager.canUndo,
      canRedo: this.historyManager.canRedo
    }
  }

  queryFormatState(name) {
    return this.formatHandler.queryCommandState(name)
  }

  // Vue适配方法
  getVueAdapter() {
    return {
      getContent: () => this.getContent(),
      setContent: (content) => this.setContent(content),
      undo: () => this.undo(),
      redo: () => this.redo(),
      lockSelection: () => this.lockSelection(),
      execFormatCommand: (name, ...args) => this.execFormatCommand(name, ...args),
      queryFormatState: (name) => this.queryFormatState(name)
    }
  }
}
